package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasGoods;
import com.kingint.mbg.bas.mapper.BasGoodsMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.tree.BasGoosTree;
import com.kingint.web.util.tree.TreeBo;
import com.kingint.web.util.tree.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 消耗品和布草/易碎品相关表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-22
 */
@Service
@Slf4j
public class BasGoodsServiceImpl extends ServiceImpl<BasGoodsMapper, BasGoods> implements BasGoodsService {

    @Resource
    private UmsAdminService adminService;

    @Resource
    private BasGoodsMapper basGoodsMapper;

    @Override
    public int insertGoodsInfo(BasGoods basGoods) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号没有关联酒店id");
            return 0;
        }
        basGoods.setCreateTime(new Date());
        basGoods.setHotelId(hotelId);
        int insert = basGoodsMapper.insert(basGoods);
        return insert;
    }

    @Override
    public int updateGoodsInfo(BasGoods basGoods) {
        int i = basGoodsMapper.updateById(basGoods);
        return i;
    }

    @Override
    public int deleteGoodsInfo(Integer id) {
        QueryWrapper<BasGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",id) .eq("status", 0);
        int count = this.count(queryWrapper);
        if(count>0){
            return 0;
        }
        BasGoods goods = new BasGoods();
        goods.setId(id);
        goods.setStatus(1);//删除
        int i = basGoodsMapper.updateById(goods);
        return i;
    }

    @Override
    public Object getGoodList(BasGoods basGoods, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
//        if (StringUtils.isEmpty(hotelId)) {
//            log.debug("该账号没有关联酒店id");
//            return null;
//        }
////        IPage<BasGoods> page = new Page<>(pageNum, pageSize);
////        QueryWrapper<BasGoods> wrapper = new QueryWrapper<>();
////        wrapper.eq("hotel_id", hotelId)
////                .eq("status", 0)
////                .eq("pid", 0);
////        IPage<BasGoods> page1 = basGoodsMapper.selectPage(page, wrapper);
////
////        QueryWrapper<BasGoods> wrapper1 = new QueryWrapper<>();
////        wrapper1.eq("hotel_id", hotelId)
////                .eq("status", 0);
////        List<BasGoods> basGoods1 = basGoodsMapper.selectList(wrapper1);
////        cancelGoods(page1.getRecords(), basGoods1);
////        return page1;
        QueryWrapper<BasGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasGoods> basGoods1 = basGoodsMapper.selectList(wrapper1);
        TreeUtil<BasGoods> basGoodsTreeUtil = new TreeUtil<BasGoods>("getId","getPid");

        List<TreeBo<BasGoods>> createTreeAsTreeEntity=null;
        List<BasGoosTree> list=new ArrayList<>();
        try {

            createTreeAsTreeEntity = basGoodsTreeUtil.createTreeAsTreeEntity(new HashSet<BasGoods>(basGoods1));
            if(createTreeAsTreeEntity.size()>0)
                treeGoods(list,createTreeAsTreeEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return K.ok().data(list);
    }

    private void treeGoods(List<BasGoosTree> list, List<TreeBo<BasGoods>> treeBoList){

        for (TreeBo<BasGoods> basGoodsTreeBo : treeBoList) {
            BasGoods title = basGoodsTreeBo.getTitle();
            BasGoosTree basGoosTree = new BasGoosTree();
            basGoosTree.setLabel(title.getGoodsName());
            basGoosTree.setId(title.getId());
            basGoosTree.setPrice(title.getPrice()!=null?title.getPrice():null);
            basGoosTree.setType(title.getType());
            basGoosTree.setUnit(title.getUnit());
            basGoosTree.setIsgoods(title.getIsgoods());
            basGoosTree.setPid(title.getPid());
            List<TreeBo<BasGoods>> children = basGoodsTreeBo.getChildren();
            if(children!=null){
                treeGoods(basGoosTree.getChildren(),children);
            }
            list.add(basGoosTree);//存数据
        }
    }
    public void cancelGoods(List<BasGoods> aClass, List<BasGoods> allList) {
        if (!CollectionUtils.isEmpty(aClass)) {
            //为一级菜单设置子菜单
            for (BasGoods info : aClass) {
                getChildList(info, allList);
            }
        }
    }

    public void getChildList(BasGoods info, List<BasGoods> aClass) {
        List<BasGoods> serviceClass = info.getGoodsList();
        if (CollectionUtils.isEmpty(serviceClass)) {
            serviceClass = new ArrayList<>();
        }
        for (BasGoods ser : aClass) {
            if (ser.getPid() == info.getId()) {
                serviceClass.add(ser);
            }
        }
        //递归查询
        if (serviceClass.size() != 0) {
            for (BasGoods child : serviceClass) {
                getChildList(child, aClass);
            }
        }
        info.setGoodsList(serviceClass);
    }
}
